package com.hualu.xmlCode.dao.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.tnc.dao.hibernate.GenericDaoHibernate;

import com.hualu.xmlCode.dao.AppVideosDao;
import com.hualu.xmlCode.model.AppVideos;

public class AppVideosDaoHibernate extends GenericDaoHibernate<AppVideos, Integer> implements AppVideosDao {

    public AppVideosDaoHibernate() {
        super(AppVideos.class);
    }
    
    
    public void saveAppVideos(AppVideos appVideos){
        this.getHibernateTemplate().save(appVideos);
    }
    
    public List<AppVideos> getAppVideosList(Integer videoType){
        
        DetachedCriteria c = DetachedCriteria.forClass(AppVideos.class);
        List<AppVideos> list = new ArrayList<AppVideos>();
        
        // 电影类别, 1 电影 , 2 电视剧, 3 片花, 4 红色经典
        if (videoType != null) {
            if (videoType == 1) {
                c.add(Restrictions.eq("videoType", 1));
            }
            if (videoType == 2) {
                c.add(Restrictions.eq("videoType", 2));
            }
            if (videoType == 3) {
                c.add(Restrictions.eq("videoType", 3));
            }
//            if (videoType == 4) {
//                c.add(Restrictions.eq("videoType", 4));
//            }
        }
        
        list = this.findByCriteria(c);
        return list;
    }
    
    
    @SuppressWarnings("unchecked")
    public List<AppVideos> findByCriteria(final DetachedCriteria detachedCriteria) {
        return (List) getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session session) throws HibernateException {
                Criteria criteria = detachedCriteria.getExecutableCriteria(session);
                return criteria.list();
            }
        });
    }
}
